package com.qinhao.loveCrowdFunding.service;

import com.qinhao.loveCrowdFunding.pojo.*;
import com.qinhao.loveCrowdFunding.req.*;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.qinhao.loveCrowdFunding.mapper.UserLoginMapper;

import java.util.List;

@Service
public class UserLoginService {
    @Autowired
    private UserLoginMapper loginMapper;

    public UserLoginDTO queryUserByLogin(UserLoginRequest loginRequest) {
        UserLoginBO userLoginBO = loginMapper.queryUserByLogin(loginRequest);
        UserLoginDTO userLoginDTO = new UserLoginDTO();
        if (userLoginBO == null) {
            return userLoginDTO;
        } else {
            BeanUtils.copyProperties(userLoginBO, userLoginDTO);
        }
        return userLoginDTO;
    }

    public void updatePassword(UpdatePasswordRequest updatePasswordRequest) {
        loginMapper.updatePasswordByPhone(updatePasswordRequest);
    }

    public Boolean updateNameById(UpdateNameByIdRequest updateNameByIdRequest) {
        UpdateNameByIdBO updateNameByIdBO = loginMapper.queryUserByUserName(updateNameByIdRequest);
        if (updateNameByIdBO == null) {
            loginMapper.updateNameById(updateNameByIdRequest);
            return true;
        }
        return false;
    }

    public GetIntegralBalanceDTO getIntegralBalanceByUserId(GetIntegralBalanceRequest getIntegralBalanceRequest) {
        GetIntegralBalanceBO integralBalanceByUserId = loginMapper.getIntegralBalanceByUserId(getIntegralBalanceRequest);
        GetIntegralBalanceDTO getIntegralBalanceDTO = GetIntegralBalanceDTO.builder().integralBalance(integralBalanceByUserId.getIntergralCount()).build();
        return getIntegralBalanceDTO;
    }

    public List<GetIntegralRecordBO> getIntegralRecord(GetIntegralRecordRequest getIntegralRecordRequest) {
        List<GetIntegralRecordBO> integralRecord = loginMapper.getIntegralRecord(getIntegralRecordRequest);
        return integralRecord;
    }
}
